package top.yrlby.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import top.yrlby.common.R;
import top.yrlby.dto.SetmealDto;
import top.yrlby.pojo.Category;
import top.yrlby.pojo.Setmeal;
import top.yrlby.service.CategoryService;
import top.yrlby.service.SetmealDishService;
import top.yrlby.service.SetmealService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author baiyang
 * @since 2022-09-05
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐信息
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功！");
    }


    @GetMapping("/page")
     public R<Page> pate(int page,int pageSize,String name){
        //分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        //创建查询条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like模糊查询
        queryWrapper.like(StringUtils.isNotBlank(name),Setmeal::getName,name);
        //添加排序条件，根据更新时间降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //MP创建的分页查询SQL给分页的个数和页数
        setmealService.page(pageInfo, queryWrapper);

        //创建page对象
        Page<SetmealDto> dtoPage = new Page<>();
        //对象拷贝，不拷贝'records'
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();
        //遍历
        List<SetmealDto> list=records.stream().map((item)->{
            //创建dto对象
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                //分类名称
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);//把获取的数据存入records
        return R.success(dtoPage);
     }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
     @GetMapping("/list")
     @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
     public R<List<Setmeal>> list(Setmeal setmeal){
         //创建条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //根据套餐分类查询 where ategoryId =
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        //根据更新时间排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //MP自动生成SQL语句
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

//    /**
//     * 删除套餐
//     * @param ids
//     * @return
//     */
//    @DeleteMapping
//    //清除setmealCache名称下,所有的缓存数据
//    @CacheEvict(value = "setmealCache", allEntries = true)
//    // redisTemplate.delete("setmealCache*")
//    public R<String> delete(@RequestParam List<Long> ids){
//        log.info("ids:{}",ids);
//        setmealService.removeWithDish(ids);
//        return R.success("套餐数据删除成功");
//    }
}

